package com.tiantianfresh.admin.controller;

import com.tiantianfresh.admin.service.CommodityAllocationService;
import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.CommodityAllocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author FANGAO
 * @date 2021/12/23 - 下午 22:57
 */
@Controller
@RequestMapping("commodityAllocation")
public class CommodityAllocationController {
    @Autowired
    private CommodityAllocationService commodityAllocationService;
    @RequestMapping("goCommodityAllocation/{cid}")
    public String goCommodityAllocation(@PathVariable Integer cid, Model model){
        model.addAttribute("comid",cid);
        return "commodity/CommodityAllocation";
    }
    @ResponseBody
    @RequestMapping("queryCommodityAllocationAll/{comid}")
    public Object queryCommodityAllocationAll(@PathVariable Integer comid){
        AJAXResult result = new AJAXResult();
        List<CommodityAllocation> commodityAllocations = commodityAllocationService.queryCommodityAllocationAll(comid);
        if (commodityAllocations!=null){
            result.setData(commodityAllocations);
            result.setSuccess(true);
        }else {
            result.setSuccess(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("insertCommodity")
    public Object insertCommodity(CommodityAllocation commodityAllocation){
        System.out.println(commodityAllocation.toString()+"------------------------");
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityAllocationService.insertCommodity(commodityAllocation));
        return result;
    }


    @ResponseBody
    @RequestMapping("updateCommodity")
    public Object updateCommodity(CommodityAllocation commodityAllocation){
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityAllocationService.updateCommodity(commodityAllocation));
        return result;
    }


    @ResponseBody
    @RequestMapping("updateState")
    public Object updateState(CommodityAllocation commodityAllocation){
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityAllocationService.updateState(commodityAllocation));
        return result;
    }
}
